﻿Imports CrystalDecisions.CrystalReports.Engine

Public Class frmViewReport
    Public pNomEtat As String
    Public pParamRPT As String

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub frmViewReport_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim szCheminRPT As String = ""
        Dim szCriteresRPT As String = ""

        ' lire les infos de l'état
        szSQL = "CALL spGET_P_EDITCRYS('REPORT','" & pNomEtat & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount < 1 Then
            MsgBox("Etat " & pNomEtat & " : non trouvé dans la table P_ETATS!", MsgBoxStyle.Critical)
            Exit Sub
        End If

        ' afficher le nom du RPT sur la barre en-tête
        Me.Text = rs.Fields("libelle").Value & "  (" & pNomEtat & ")"
        szCheminRPT = My.Settings.DossierRoot & "REPORT\" & pNomEtat
        szCriteresRPT = FormaterSelection(nv(rs.Fields("selection").Value))
        rs.Close()

        Select Case pNomEtat.ToUpper
            Case "AXA-FACTURES.RPT"
                ' Edition Factures AXA
                Call fEdition_Factures_AXA()
                Exit Sub

            Case "AXA-RDV-CHANTIERS.RPT"
                Call fEdition_RDV_AXA("N")
                ' nom réel de l'état
                szCheminRPT = My.Settings.DossierRoot & "REPORT\" & "AXA-CHANTIERS.RPT"

            Case "AXA-RDV-AUTRES.RPT"
                Call fEdition_RDV_AXA("M")
                ' nom réel de l'état
                szCheminRPT = My.Settings.DossierRoot & "REPORT\" & "AXA-CHANTIERS.RPT"

            Case "Soldes_Clients_parTVA.rpt"
                ' Calcul soldes Factures à la date
                szSQL = "CALL spUPD_FACTURES_SOLDE('" & gSocieteCode & "','" & Format(gDate_debut, "yyyy/MM/dd") & "')"
                ExecQuery(ctDSN_DEVFAC, szSQL)

            Case "STAT_ANALYSE_DEVIS.RPT"
                ' Edition Analyse Devis & Factures
                Call fAnalyseDevis()

        End Select



        ' OUVERTURE du REPORT
        Dim rpt As New ReportDocument
        Try
            rpt.Load(szCheminRPT)
        Catch ex As Exception
            MsgBox(ex.Message & " : " & vbCrLf & szCheminRPT, vbCritical)
            Exit Sub
        End Try
        ' définir la sélection
        If szCriteresRPT.Length > 0 Then
            rpt.RecordSelectionFormula = szCriteresRPT
        End If
        ' definir le(s) paramètre(s) éventuel(s)
        If pParamRPT.Length > 0 Then
            rpt.SetParameterValue(0, pParamRPT)
        End If
        ' Editer
        CrystalReportViewer1.ReportSource = rpt

    End Sub

    Private Function FormaterSelection(szSelection As String) As String

        If szSelection.Length > 0 Then
            szSQL = Replace(szSelection, "gSociete", gSocieteCode)
            szSQL = Replace(szSQL, "gNoDossier", gNoDossier)
            szSQL = Replace(szSQL, "gNoCli", gCD_CLI)
            szSQL = Replace(szSQL, "gCodeUser", gUserCode)
            szSQL = Replace(szSQL, "gDate_deb", Format(gDate_debut, "yyyy/MM/dd"))
            szSQL = Replace(szSQL, "gDate_fin", Format(gDate_fin, "yyyy/MM/dd"))
        Else
            szSQL = ""
        End If
        FormaterSelection = szSQL

    End Function

    Sub fEdition_Factures_AXA()
        Dim nTotalDossiers As Integer
        Dim szNomRPT As String
        Dim szFormula As String

        ' Lire toutes les factures d'AXA de date à date
        szSQL = "CALL spGET_FACTURE_byDates('" & gSocieteCode & "','" & Format(gDate_debut, "yyyy-mm-dd") & _
                "','" & Format(gDate_fin, "yyyy-mm-dd") & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        nTotalDossiers = 0
        Do Until rs.EOF
            If rs.Fields("groupe").Value = "A" Then
                nTotalDossiers = nTotalDossiers + 1
                ' la facture a des sous-totaux ?
                If HasSubTotal("FACTURE_LIG", rs.Fields("NO_ID").Value) = 0 Then
                    szNomRPT = "Facture.rpt"
                Else
                    szNomRPT = "FactureST.rpt"
                End If
                szFormula = "{FACTURE.societe}='" & gSocieteCode & "' AND {FACTURE.NO_ID}=" & rs.Fields("NO_ID").Value

                PrintToPrinter(szSQL, szFormula, gImpEntete)

            End If
            rs.MoveNext()
        Loop
        MsgBox(Str(nTotalDossiers) & " factures ont été éditées")
        rs.Close()

    End Sub

    Sub fEdition_RDV_AXA(ByVal szListe As String)

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        'Création de la Table Temporaire
        ExecQuery(ctDSN_DEVFAC, "CALL spDEL_T_TABLE('T_IMP_RDV_JOUR','" & gUserCode & "')")

        ' générer les RDV de la societe
        szSQL = "CALL spINS_T_IMP_RDV_JOUR_AXA('" & gSocieteCode & "','" & Format(gDate_debut, "yyyy/MM/dd") & "','" & _
                      Format(gDate_fin, "yyyy/MM/dd") & "','" & szListe & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSql)

        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

    Sub fAnalyseDevis()

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        'Création de la Table Temporaire
        szSQL = "CALL spDEL_T_TABLE('T_ANALYSE_DEVIS','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSql)

        szSQL = "CALL spINS_T_ANALYSE_DEVIS('" & gSocieteCode & "','" & Format(gDate_debut, "yyyy/MM/dd") & "','" & _
                Format(gDate_fin, "yyyy/MM/dd") & "','" & gUserCode & "');"
        ExecQuery(ctDSN_DEVFAC, szSql)

        Me.Cursor = System.Windows.Forms.Cursors.Default

    End Sub

    Private Function HasSubTotal(ByVal pSousRub As String, ByVal pNoDossier As Integer) As Integer

        HasSubTotal = 0
        szSQL = "CALL spGET_LIGNES('" & gSocieteCode & "','" & pSousRub & "'," & pNoDossier & ",0)"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount < 1 Then
            rs.Close()
            Exit Function
        End If

        Do While Not rs.EOF
            If UCase(rs.Fields("REF").Value) = "ST" Then
                HasSubTotal = HasSubTotal + 1
            End If
            rs.MoveNext()
        Loop
        rs.Close()

    End Function

End Class